import i18n from "@/utils/i18n/i18n";
import {checkPermi} from "@/utils/permission";

const DEFECT_TEMP_TAB_KEY = "defect-temp-tab-key";
/** 缺陷类型-任务 */
export const DEFECT_TYPE_TASK = "TASK";
/** 缺陷类型-需求 */
export const DEFECT_TYPE_DEMAND = "DEMAND";
/** 缺陷类型-BUG */
export const DEFECT_TYPE_BUG = "BUG";

/** 处理中状态 */
export const PROCESSING_STATE = 'PROCESSING';
/** 待验证 */
export const AUDIT_STATE = 'AUDIT';
/** 已解决 */
export const RESOLVED_STATE = 'RESOLVED';
/** 已驳回 */
export const REJECTED_STATE = 'REJECTED';
/** 关闭状态 */
export const CLOSE_STATE = 'CLOSED';
	
/** 缺陷标签类型 */
export const DEFECT_TAB_BAR_TYPE = {
	  LIST: 'list',
	  RADIO: 'radio'
	}
	
/** 计算缺陷存活时间 */
export function lifeTime (defect){
  let passTime = defect.handlePassTime ? new Date(defect.handlePassTime.replaceAll('-','/')): new Date();
  let startTime = defect.handleStartTime ? new Date(defect.handleStartTime.replaceAll('-','/')): new Date();
  let lifeTime = passTime.getTime() - startTime.getTime();
  const day = Math.floor(lifeTime/(1000 * 60 * 60 * 24));
  lifeTime = lifeTime%(1000 * 60 * 60 * 24);
  const hour = Math.floor(lifeTime/(1000 * 60 * 60));
  lifeTime = lifeTime%(1000 * 60 * 60);
  const minute = Math.floor(lifeTime/(1000 * 60));
  let ret = [];
  if(day) ret.push(day+i18n.t('day'));
  if(hour) ret.push(hour+i18n.t('hour'));
  ret.push(minute+i18n.t('minute'));
  return ret.join(' ');
}

/** 设置缺陷临时Tab标签数据 */
export function setDefectTempTab(data) {
  uni.setStorageSync(DEFECT_TEMP_TAB_KEY, data);
}
/** 获取缺陷临时Tab标签数据 */
export function getDefectTempTab() {
	return uni.getStorageSync(DEFECT_TEMP_TAB_KEY);
}
/** 移除缺陷临时Tab标签数据 */
export function removeDefectTempTab() {
	return uni.removeStorageSync(DEFECT_TEMP_TAB_KEY);
}

/** 检查缺陷指派权限 */
export function checkAssignPermi(defect, memberId) {
	return (defect.defectStateName!=CLOSE_STATE && defect.defectStateName!=RESOLVED_STATE && checkPermi(['system:defect:assign']) &&
	(
		(defect.createById && defect.createById == memberId) ||
		(defect.handleBy && defect.handleBy.indexOf(memberId)!==-1)
	));
}
/** 检查缺陷修复权限 */
export function checkRepairPermi(defect, memberId) {
  return ((defect.defectStateName==PROCESSING_STATE || defect.defectStateName==REJECTED_STATE) && checkPermi(['system:defect:repair']) &&
  		(defect.handleBy && defect.handleBy.indexOf(memberId)!==-1));
}
/** 检查缺陷驳回权限 */
export function checkRejectPermi(defect,memberId) {
  return (defect.defectStateName==AUDIT_STATE && checkPermi(['system:defect:reject']) &&
  	(defect.handleBy && defect.handleBy.indexOf(memberId)!==-1));
}
/** 检查缺陷通过权限 */
export function checkPassPermi(defect,memberId) {
  return (defect.defectStateName==AUDIT_STATE && checkPermi(['system:defect:pass']) &&
  		(defect.handleBy && defect.handleBy.indexOf(memberId)!==-1));
}
/** 检查缺陷关闭权限 */
export function checkClosePermi(defect) {
  return (defect.defectStateName!=CLOSE_STATE && defect.defectStateName!=RESOLVED_STATE && checkPermi(['system:defect:close']));
}
/** 检查缺陷开启权限 */
export function checkOpenPermi(defect) {
  return (defect.defectStateName==CLOSE_STATE && checkPermi(['system:defect:open']));
}
/** 检查缺陷删除权限 */
export function checkDeletePermi(defect,memberId) {
  return (defect.createById==memberId || checkPermi(['system:defect:remove']));
}
/** 检查缺陷编辑权限 */
export function checkEditPermi(defect,memberId) {
  return (defect.createById==memberId || checkPermi(['system:defect:edit']));
}


